using micromsg;
using MicroMsg.Common.Utils;
using MicroMsg.Protocol;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Manager
{
	public class ChatRoomMgr
	{
		private const string TAG = "ChatRoomMgr";
		private const string USERNAME_SEPARATOR = "、";
		private const int ROOM_MUTE_ON = 0;
		private const int ROOM_MUTE_OFF = 1;
		private static Contact respToContact(CreateChatRoomResponse resp)
		{
			return new Contact
			{
				strNickName = resp.Topic.String,
				strPYInitial = resp.PYInitial.String,
				strQuanPin = resp.QuanPin.String,
				strUsrName = resp.ChatRoomName.String,
				strChatRoomOwner = AccountMgr.curUserName
			};
		}
		private static Contact memberToContact(MemberResp member)
		{
			Contact contact = StorageMgr.contact.method_0(member.MemberName.String);
			if (contact == null)
			{
				contact = new Contact();
				contact.strUsrName = member.MemberName.String;
			}
			contact.strNickName = member.NickName.String;
			contact.strPYInitial = member.PYInitial.String;
			contact.strQuanPin = member.QuanPin.String;
			contact.nSex = member.Sex;
			contact.strRemark = member.Remark.String;
			contact.strRemarkPYInitial = member.RemarkPYInitial.String;
			contact.strRemarkQuanPin = member.RemarkQuanPin.String;
			contact.nContactType = member.ContactType;
			contact.nPersonalCard = member.PersonalCard;
			contact.strProvince = member.Province;
			contact.strCity = member.City;
			contact.strSignature = member.Signature;
			return contact;
		}
		public static string userNameListToString(List<string> userNameList)
		{
			string text = "";
			for (int i = 0; i < userNameList.get_Count(); i++)
			{
				text += userNameList.get_Item(i);
				if (i < userNameList.get_Count() - 1)
				{
					text += "、";
				}
			}
			return text;
		}
		public static string userNameListToNickNameString(List<string> userNameList)
		{
			string text = "";
			for (int i = 0; i < userNameList.get_Count(); i++)
			{
				text += ContactHelper.getDisplayName(userNameList.get_Item(i), null);
				if (i < userNameList.get_Count() - 1)
				{
					text += "、";
				}
			}
			return text;
		}
		public static List<string> stringToUsernameList(string userName)
		{
			List<string> list = new List<string>();
			string[] array = userName.Split("、".ToCharArray());
			for (int i = 0; i < array.Length; i++)
			{
				list.Add(array[i]);
			}
			return list;
		}
		public static RetConst checkMemberStatusFromMemberList(List<string> invalidUsernameList, List<string> memeberBlackList, List<string> notExistUserList, List<string> verifyUserList, IList<MemberResp> memberList)
		{
			RetConst result = RetConst.MM_OK;
			using (IEnumerator<MemberResp> enumerator = memberList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					MemberResp current = enumerator.get_Current();
					uint memberStatus = current.MemberStatus;
					string @string = current.MemberName.String;
					if (memberStatus != 0u)
					{
						if (memberStatus == 3u)
						{
							Log.smethod_5("ChatRoomMgr", " blacklist : " + @string);
							memeberBlackList.Add(@string);
							result = RetConst.MM_ERR_BLACKLIST;
						}
						else
						{
							if (memberStatus == 1u)
							{
								Log.smethod_5("ChatRoomMgr", " not user : " + @string);
								notExistUserList.Add(@string);
								result = RetConst.MM_ERR_NOTCHATROOMCONTACT;
							}
							else
							{
								if (memberStatus == 2u)
								{
									Log.smethod_5("ChatRoomMgr", " invalid username : " + @string);
									invalidUsernameList.Add(@string);
									result = RetConst.MM_ERR_USERNAMEINVALID;
								}
								else
								{
									if (memberStatus == 4u)
									{
										Log.smethod_5("ChatRoomMgr", " verify user : " + @string);
										verifyUserList.Add(@string);
										result = RetConst.MM_ERR_NEED_VERIFY_USER;
									}
									else
									{
										Log.smethod_1("ChatRoomMgr", "unknown member status : status = " + memberStatus);
										result = RetConst.MM_ERR_SYS;
									}
								}
							}
						}
					}
				}
			}
			return result;
		}
		public static bool mergUserListByChatRoomName(string chatRoomName, List<string> userNameList)
		{
			ChatRoom chatRoom = StorageMgr.chatRoom.method_7(chatRoomName);
			if (chatRoom != null)
			{
				List<string> list = ChatRoomMgr.stringToUsernameList(chatRoom.strUserNameList);
				using (List<string>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						string current = enumerator.get_Current();
						if (!userNameList.Contains(current))
						{
							userNameList.Add(current);
						}
					}
				}
			}
			return ChatRoomMgr.replaceUserListByChatRoomName(chatRoomName, userNameList);
		}
		public static bool reMergUserListByChatRoomName(string chatRoomName, List<string> userNameList)
		{
			ChatRoom chatRoom = StorageMgr.chatRoom.method_7(chatRoomName);
			List<string> list = new List<string>();
			if (chatRoom != null)
			{
				list = ChatRoomMgr.stringToUsernameList(chatRoom.strUserNameList);
				using (List<string>.Enumerator enumerator = userNameList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						string current = enumerator.get_Current();
						if (list.Contains(current))
						{
							list.Remove(current);
						}
					}
				}
			}
			return ChatRoomMgr.replaceUserListByChatRoomName(chatRoomName, list);
		}
		public static bool replaceUserListByChatRoomName(string chatRoomName, List<string> userNameList)
		{
			ChatRoom chatRoom = new ChatRoom();
			chatRoom.strChatRoomName = chatRoomName;
			chatRoom.strUserNameList = ChatRoomMgr.userNameListToString(userNameList);
			chatRoom.nTime = (uint)Util.getNowSeconds();
			if (!StorageMgr.chatRoom.update(chatRoom))
			{
				Log.smethod_5("ChatRoomMgr", "updateChatRoom failed");
				return false;
			}
			return true;
		}
		public static bool createChatroom(CreateChatRoomResponse createInfo)
		{
			if (ContactMgr.getUserType(createInfo.ChatRoomName.String) != ContactMgr.UserType.UserTypeChatRoom || createInfo.MemberListList.get_Count() == 0)
			{
				Log.smethod_1("ChatRoomMgr", string.Concat(new object[]
				{
					"CreateChatroom: room:",
					createInfo.ChatRoomName.String,
					" listCnt:",
					createInfo.MemberListList.get_Count()
				}));
				return false;
			}
			List<Contact> list = new List<Contact>();
			List<string> list2 = new List<string>();
			List<string> list3 = new List<string>();
			Contact contact = ChatRoomMgr.respToContact(createInfo);
			list.Add(contact);
			using (IEnumerator<MemberResp> enumerator = createInfo.MemberListList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					MemberResp current = enumerator.get_Current();
					Contact contact2 = ChatRoomMgr.memberToContact(current);
					ContactMgr.setChatroomContact(contact2, true);
					list.Add(contact2);
					if (contact2.strUsrName != AccountMgr.getCurAccount().strUsrName)
					{
						list2.Add(contact2.strUsrName);
					}
				}
			}
			list3.AddRange(list2);
			list3.Add(AccountMgr.getCurAccount().strUsrName);
			if (!StorageMgr.contact.updateList(list))
			{
				Log.smethod_5("ChatRoomMgr", "createChatroom failed");
				return false;
			}
			if (!ChatRoomMgr.mergUserListByChatRoomName(contact.strUsrName, list3))
			{
				Log.smethod_5("ChatRoomMgr", "createChatroom failed");
				return false;
			}
			return ChatRoomMgr.addChatRoomNotifyMsg(createInfo.ChatRoomName.String, list2);
		}
		private static bool addChatRoomNotifyMsg(string chatRoomName, List<string> userNameList)
		{
			ChatMsg chatMsg = new ChatMsg();
			chatMsg.nMsgSvrID = -1;
			chatMsg.nMsgType = 10000;
			chatMsg.strTalker = chatRoomName;
			chatMsg.nCreateTime = (long)Util.getNowSeconds();
			string text = ChatRoomMgr.userNameListToNickNameString(userNameList);
			chatMsg.strMsg = string.Format(strings.ChatInviteInfo, text);
			chatMsg.nIsSender = 0;
			chatMsg.nStatus = 2;
			return StorageMgr.chatMsg.addMsg(chatMsg);
		}
		public static bool delChatroomMember(string chatRoomName, DelChatRoomMemberResponse memberInfo)
		{
			Contact contact = StorageMgr.contact.method_0(chatRoomName);
			StorageMgr.contact.modify(contact);
			HeadImageMgr.downLoadHeadImage(contact.strUsrName, null);
			if (ContactMgr.getUserType(chatRoomName) == ContactMgr.UserType.UserTypeChatRoom && memberInfo.MemberListList.get_Count() != 0)
			{
				List<string> list = new List<string>();
				using (IEnumerator<DelMemberResp> enumerator = memberInfo.MemberListList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						DelMemberResp current = enumerator.get_Current();
						Contact contact2 = StorageMgr.contact.method_0(current.MemberName.String);
						list.Add(contact2.strUsrName);
					}
				}
				return ChatRoomMgr.reMergUserListByChatRoomName(chatRoomName, list);
			}
			Log.smethod_1("ChatRoomMgr", string.Concat(new object[]
			{
				"DelMemberChatroom: room:",
				chatRoomName,
				" listCnt:",
				memberInfo.MemberListList.get_Count()
			}));
			return false;
		}
		public static bool addChatroomMember(string chatRoomName, AddChatRoomMemberResponse memberInfo)
		{
			if (ContactMgr.getUserType(chatRoomName) != ContactMgr.UserType.UserTypeChatRoom || memberInfo.MemberListList.get_Count() == 0)
			{
				Log.smethod_1("ChatRoomMgr", string.Concat(new object[]
				{
					"CreateChatroom: room:",
					chatRoomName,
					" listCnt:",
					memberInfo.MemberListList.get_Count()
				}));
				return false;
			}
			List<Contact> list = new List<Contact>();
			List<string> list2 = new List<string>();
			using (IEnumerator<MemberResp> enumerator = memberInfo.MemberListList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					MemberResp current = enumerator.get_Current();
					Contact contact = ChatRoomMgr.memberToContact(current);
					ContactMgr.setChatroomContact(contact, true);
					list.Add(contact);
					list2.Add(contact.strUsrName);
				}
			}
			if (!StorageMgr.contact.updateList(list))
			{
				Log.smethod_5("ChatRoomMgr", "addChatroomMember failed");
				return false;
			}
			return ChatRoomMgr.mergUserListByChatRoomName(chatRoomName, list2);
		}
		public static List<string> getChatRoomMemberNickNameList(string chatRoomName)
		{
			List<Contact> chatRoomMemberContactList = ChatRoomMgr.getChatRoomMemberContactList(chatRoomName);
			if (chatRoomMemberContactList == null)
			{
				Log.smethod_1("ChatRoomMgr", "getChatRoomMemberContactList returns null");
				return null;
			}
			List<string> list = new List<string>();
			using (List<Contact>.Enumerator enumerator = chatRoomMemberContactList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Contact current = enumerator.get_Current();
					list.Add(current.strNickName);
				}
			}
			return list;
		}
		public static List<Contact> getChatRoomMemberContactList(string chatRoomName)
		{
			List<string> membersByChatRoomName = ChatRoomMgr.getMembersByChatRoomName(chatRoomName);
			if (membersByChatRoomName == null)
			{
				Log.smethod_1("ChatRoomMgr", "getListUserNameAndNickName,userNameList==null");
				return null;
			}
			return StorageMgr.contact.getByUserNameList(membersByChatRoomName);
		}
		public static List<string> getMembersByChatRoomName(string chatRoomName)
		{
			if (ContactMgr.getUserType(chatRoomName) != ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_1("ChatRoomMgr", "getMembersByChatRoomName,room:" + chatRoomName);
				return null;
			}
			return ChatRoomMgr.getMemberListByChatroomName(chatRoomName);
		}
		public static List<Contact> getFriendContatsFromChatRoomName(string strUsrName)
		{
			List<string> membersByChatRoomName = ChatRoomMgr.getMembersByChatRoomName(strUsrName);
			if (membersByChatRoomName == null)
			{
				return null;
			}
			List<Contact> list = new List<Contact>();
			using (List<string>.Enumerator enumerator = membersByChatRoomName.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					Contact contact = StorageMgr.contact.method_0(current);
					if (contact != null && !ContactMgr.isBlackList(contact) && ContactMgr.isContact(contact))
					{
						list.Add(contact);
					}
				}
			}
			return list;
		}
		public static List<string> getMembersByGroupCardName(string groupcardName)
		{
			if (ContactMgr.getUserType(groupcardName) != ContactMgr.UserType.UserTypeGroupCard)
			{
				Log.smethod_1("ChatRoomMgr", "getMembersByGroupCardName,groupcardName" + groupcardName);
				return null;
			}
			return ChatRoomMgr.getMemberListByChatroomName(groupcardName);
		}
		private static List<string> getMemberListByChatroomName(string chatRoomName)
		{
			ChatRoom chatRoom = StorageMgr.chatRoom.method_7(chatRoomName);
			if (chatRoom == null)
			{
				Log.smethod_1("ChatRoomMgr", "error ,getMemberListByChatroomName failed,chatRoomName=" + chatRoomName);
				StorageMgr.chatMsg.dumpTable();
				StorageMgr.contact.dumpTable();
				StorageMgr.chatRoom.dumpTable();
				return null;
			}
			return ChatRoomMgr.stringToUsernameList(chatRoom.strUserNameList);
		}
		public static bool addGroupCardContact(GroupCardRequest groupCardRequest_0, GroupCardResponse resp)
		{
			Contact contact = StorageMgr.contact.method_0(resp.GroupUserName);
			if (contact == null)
			{
				contact = new Contact();
				contact.strUsrName = resp.GroupUserName;
			}
			contact.strNickName = groupCardRequest_0.GroupNickName;
			ContactMgr.setContact(contact, true);
			StorageMgr.contact.update(contact);
			List<string> list = new List<string>();
			using (IEnumerator<RoomInfo> enumerator = groupCardRequest_0.MemberListList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					RoomInfo current = enumerator.get_Current();
					list.Add(current.UserName.String);
				}
			}
			return ChatRoomMgr.mergUserListByChatRoomName(contact.strUsrName, list);
		}
		public static bool addToContact(string chatRoomName, string strRemarkName)
		{
			if (string.IsNullOrEmpty(chatRoomName))
			{
				return false;
			}
			if (ContactMgr.getUserType(chatRoomName) != ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_1("ChatRoomMgr", "invalid chatRoomName" + chatRoomName);
				return false;
			}
			Contact contact = StorageMgr.contact.method_0(chatRoomName);
			if (contact == null)
			{
				return false;
			}
			if (string.IsNullOrEmpty(strRemarkName))
			{
				contact.strRemark = "";
				contact.strRemarkPYInitial = "";
				contact.strRemarkQuanPin = "";
			}
			else
			{
				contact.strRemark = strRemarkName;
				contact.strRemarkPYInitial = SortStringsByAlpha.ConvertStringToPinyinInitial(strRemarkName);
				contact.strRemarkQuanPin = GetPinYin.ConvertStrToQuanPin(strRemarkName);
			}
			ContactMgr.setContact(contact, true);
			if (!ContactMgr.operationModContact(contact))
			{
				Log.smethod_1("ChatRoomMgr", "OpModContact failed when username=" + contact.strUsrName);
				return false;
			}
			return true;
		}
		public static bool setChatRoomTopic(string chatRoomName, string chatRoomTopic)
		{
			if (string.IsNullOrEmpty(chatRoomName) || chatRoomTopic == null)
			{
				return false;
			}
			if (ContactMgr.getUserType(chatRoomName) != ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_1("ChatRoomMgr", "invalid chatRoomName" + chatRoomName);
				return false;
			}
			Contact contact = StorageMgr.contact.method_0(chatRoomName);
			if (contact == null)
			{
				return false;
			}
			contact.strNickName = chatRoomTopic;
			StorageMgr.contact.modify(contact);
			return OpLogMgr.OpModChatRoomTopic(chatRoomName, chatRoomTopic);
		}
		public static bool GetChatRoomShowTooMuchMsgNotify(string chatRoomName, int newMsgNum)
		{
			int chatRoomTooMuchMsgNotifyNumber = ChatRoomMgr.GetChatRoomTooMuchMsgNotifyNumber(chatRoomName);
			return newMsgNum > 40 && ChatRoomMgr.GetChatRoomNotify(chatRoomName) && chatRoomTooMuchMsgNotifyNumber < 3;
		}
		public static void IncreaseChatRoomTooMuchMsgNotifyNumber(string chatRoomName)
		{
			int chatRoomTooMuchMsgNotifyNumber = ChatRoomMgr.GetChatRoomTooMuchMsgNotifyNumber(chatRoomName);
			if (chatRoomTooMuchMsgNotifyNumber >= 0 && chatRoomTooMuchMsgNotifyNumber < 3)
			{
				ChatRoomMgr.SetChatRoomTooMuchMsgNotifyNumber(chatRoomName, chatRoomTooMuchMsgNotifyNumber + 1);
			}
		}
		public static int GetChatRoomTooMuchMsgNotifyNumber(string chatRoomName)
		{
			if (string.IsNullOrEmpty(chatRoomName))
			{
				return 2147483647;
			}
			if (ContactMgr.getUserType(chatRoomName) != ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_1("ChatRoomMgr", "invalid chatRoomName" + chatRoomName);
				return 2147483647;
			}
			Contact contact = StorageMgr.contact.method_0(chatRoomName);
			if (contact == null)
			{
				return 2147483647;
			}
			return contact.nTooMuchMsgNotifyNumber;
		}
		public static bool SetChatRoomTooMuchMsgNotifyNumber(string chatRoomName, int int_0)
		{
			if (string.IsNullOrEmpty(chatRoomName))
			{
				return false;
			}
			if (ContactMgr.getUserType(chatRoomName) != ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_1("ChatRoomMgr", "invalid chatRoomName" + chatRoomName);
				return false;
			}
			Contact contact = StorageMgr.contact.method_0(chatRoomName);
			if (contact == null)
			{
				return false;
			}
			if (int_0 < 0 && int_0 == contact.nTooMuchMsgNotifyNumber)
			{
				return false;
			}
			contact.nTooMuchMsgNotifyNumber = int_0;
			StorageMgr.contact.modify(contact);
			return true;
		}
		public static bool GetChatRoomNotify(string chatRoomName)
		{
			if (string.IsNullOrEmpty(chatRoomName))
			{
				return false;
			}
			if (ContactMgr.getUserType(chatRoomName) != ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_1("ChatRoomMgr", "invalid chatRoomName" + chatRoomName);
				return false;
			}
			Contact contact = StorageMgr.contact.method_0(chatRoomName);
			return contact != null && contact.nChatRoomNotify != 0u;
		}
		public static bool SetModChatRoomNotify(string chatRoomName, bool enable)
		{
			if (string.IsNullOrEmpty(chatRoomName))
			{
				return false;
			}
			if (ContactMgr.getUserType(chatRoomName) != ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_1("ChatRoomMgr", "invalid chatRoomName" + chatRoomName);
				return false;
			}
			int num = enable ? 1 : 0;
			Contact contact = StorageMgr.contact.method_0(chatRoomName);
			if (contact == null)
			{
				return false;
			}
			contact.nChatRoomNotify = (uint)num;
			StorageMgr.contact.modify(contact);
			return OpLogMgr.OpModChatRoomNotify(chatRoomName, (uint)num);
		}
		public static bool quitChatRoom(string chatRoomName)
		{
			if (string.IsNullOrEmpty(chatRoomName))
			{
				return false;
			}
			if (ContactMgr.getUserType(chatRoomName) != ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_1("ChatRoomMgr", "invalid chatRoomName" + chatRoomName);
				return false;
			}
			if (StorageMgr.contact.method_0(chatRoomName) == null)
			{
				return false;
			}
			ChatMsgMgr.deleteMsgByTalker(chatRoomName);
			if (!OpLogMgr.OpDelChatContact(chatRoomName))
			{
				Log.smethod_1("ChatRoomMgr", "OpDelChatContact failed when chatRoomName" + chatRoomName);
				return false;
			}
			StorageMgr.converation.method_5(chatRoomName);
			if (!OpLogMgr.OpDelContact(chatRoomName))
			{
				Log.smethod_1("ChatRoomMgr", "OpDelContact failed when chatRoomName" + chatRoomName);
				return false;
			}
			if (!OpLogMgr.OpQuitChatRoom(chatRoomName))
			{
				Log.smethod_1("ChatRoomMgr", "OpQuitChatRoom failed when chatRoomName" + chatRoomName);
				return false;
			}
			ChatRoomMgr.deleteWholeChatroom(chatRoomName);
			return true;
		}
		private static bool deleteWholeChatroom(string chatRoomName)
		{
			if (ContactMgr.getUserType(chatRoomName) != ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_1("ChatRoomMgr", "deleteWholeChatroom,room:" + chatRoomName);
				return false;
			}
			if (!StorageMgr.contact.method_3(chatRoomName))
			{
				Log.smethod_1("ChatRoomMgr", "del contact failed,room:" + chatRoomName);
				return false;
			}
			if (!StorageMgr.chatRoom.method_8(chatRoomName))
			{
				Log.smethod_1("ChatRoomMgr", "del chatRoom failed,room:" + chatRoomName);
				return false;
			}
			return true;
		}
	}
}
